﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Light.Framework.Castle;
using Light.Utility;

namespace WxPayAPI
{
    /// <summary>
    /// 支付结果通知回调处理类
    /// 负责接收微信支付后台发送的支付结果并对订单有效性进行验证，将验证结果反馈给微信支付后台
    /// </summary>
    public class ResultNotify<ITComponent> : Notify where ITComponent : IOrderComponent
    {
        public string ordercode = "";

        public ResultNotify(Page page)
            : base(page)
        {
        }

        public override WxPayData ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();
            object objv = notifyData.GetValue("transaction_id");
            WxPayData res = new WxPayData();
            string transaction_id =objv==null?null:objv.ToString();
            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在，则立即返回结果给微信支付后台 
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
                //page.Response.Write(res.ToXml());
                //page.Response.End();
            }
            else if (!QueryOrder(transaction_id)) //查询订单，判断订单真实性
            {
                //若订单查询失败，则立即返回结果给微信支付后台 
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
                //page.Response.Write(res.ToXml());
                //page.Response.End();
            }
            else//查询订单成功
            {
                ordercode = notifyData.GetValue("out_trade_no").ToString(); 
                IOrderComponent icomponet = (IOrderComponent)ServerApplication.ComponentFactory.Create<ITComponent>();
                IOrder order = icomponet.GetByOrderCode(ordercode);
                order.PayStatus = PayStatus.Succeed;
                OperateResult result = icomponet.UpdateByOrderCode(order);

                if (result.IsSucced)
                {
                    res.SetValue("return_code", "SUCCESS");
                    res.SetValue("return_msg", "OK");
                }
                else
                {
                    res.SetValue("return_code", "FAIL");
                    res.SetValue("return_msg", "FAIL");
                }
                //page.Response.Write(res.ToXml());
                //page.Response.End();
            }
            return res;
            //page.Response.Write(res.ToXml());
            //page.Response.End();
        }

        //查询订单
        private bool QueryOrder(string transaction_id)
        {
            WxPayData req = new WxPayData();
            req.SetValue("transaction_id", transaction_id);
            WxPayData res = WxPayApi.OrderQuery(req, 6);
            if (res.GetValue("return_code").ToString() == "SUCCESS" &&
                res.GetValue("result_code").ToString() == "SUCCESS")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}